//
//  MQTabBar.swift
//  MQWeibo
//
//  Created by 毛强 on 15/12/27.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class MQTabBar: UITabBar {

    var composeButtonClickClouser: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加button监听事件
    @objc private func composeButtonClick(){
        composeButtonClickClouser?()
    }

    private lazy var composeButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        //自适应大小
        button.sizeToFit()
        return button
    }()
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center.x = bounds.size.width * 0.5
        composeButton.center.y = bounds.size.height * 0.5
        
        let childW = bounds.size.width * 0.2
        var index = 0
        
        for value in subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = childW
                value.frame.origin.x = childW * CGFloat(index)
                
                index++
                
                if index == 2 {
                    index++
                }
            }
        }
    }
}
